package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IBcLogService;
import com.wondertek.poms.dao.po.BcLog;
import com.wondertek.poms.dao.pojo.BcProcessLogPojo;
import com.wondertek.poms.dao.pojo.BcRecodePojo;
import com.wondertek.poms.dao.repo.IBcLogDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: IBcLogServiceImpl
 * @Description: TODO
 * @Author MrYuan
 * @Date: 2021/1/28 15:33
 * @Version 1.0
 */
@Service
@Scope("prototype")
public class IBcLogServiceImpl extends ServiceImpl<BcLog, Long> implements IBcLogService {
    @Autowired
    IBcLogDao bcLogDao;
    @Autowired
    private EntityManager entityManager;

    /*/**
     * @Author MrYuan
     * @Date: 2021/1/29 16:10
     * @Description: 根据页面条件返回历史纪录信息，带分页
     * @params:  [entity, page, rows, sord, sidx]
     * @return: org.springframework.data.domain.Page<com.wondertek.poms.dao.vo.history.BcRecodeVo>
     */
    @Override
    public Page<BcRecodePojo> findAll(BcRecodePojo entity, int page, int rows, String sord, String sidx) {
        StringBuffer condition = sqlStringWhere(entity, sord, sidx);
        Object o = entityManager
                .createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
        int count = Convert.toInt(o);
        Pageable pageable = getPageable(page - 1, rows, sord, sidx);

        if (count > 0) {
            Query query = entityManager.createNativeQuery(condition.toString());
            query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            query.setMaxResults(pageable.getPageSize());
            List<Object[]> resultList = query.getResultList();
            List<BcRecodePojo> list = getList(resultList);
            PageImpl<BcRecodePojo> result = new PageImpl<>(list, pageable, count);
            entityManager.close();
            return result;
        }
        entityManager.close();
        return Page.empty(pageable);
    }
/*/**
 * @Author MrYuan
 * @Date: 2021/2/1 9:39
 * @Description: 播控流程日志
 * @params:  [bcProcessLog, page, rows, sord, sidx]
 * @return: org.springframework.data.domain.Page<com.wondertek.poms.dao.vo.history.BcRecodeVo>
 */
    @Override
    public Page<BcProcessLogPojo> findAll(BcProcessLogPojo bcProcessLog, int page, int rows, String sord, String sidx) {

        StringBuffer condition = sqlStringWhere(bcProcessLog, sord, sidx);
        Object o = entityManager
                .createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
        int count = Convert.toInt(o);
        Pageable pageable = getPageable(page - 1, rows, sord, sidx);
        if (count > 0) {
            Query query = entityManager.createNativeQuery(condition.toString());
            query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            query.setMaxResults(pageable.getPageSize());
            List<Object[]> resultList = query.getResultList();
            List<BcProcessLogPojo> list = getProcessList(resultList);
            PageImpl<BcProcessLogPojo> result = new PageImpl<>(list, pageable, count);
            entityManager.close();
            return result;
        }
        entityManager.close();
        return Page.empty(pageable);
    }

    private List<BcRecodePojo> getList(List<Object[]> resultList) {
        List<BcRecodePojo> list = new ArrayList<>();
        for (Object[] obj : resultList) {
            BcRecodePojo pojo = new BcRecodePojo();
            pojo.setPrdContId(Convert.toLong(obj[0]));
            pojo.setContentId(Convert.toInt(obj[1]));
            pojo.setContentName(Convert.toStr(obj[2]));
            pojo.setBcName(Convert.toStr(obj[3]));
            pojo.setBcPerson(Convert.toStr(obj[4]));
            pojo.setBcTime(Convert.toStr(obj[5]));
            pojo.setBcStatus(Convert.toInt(obj[6]));
            list.add(pojo);
        }
        return list;
    }
    private List<BcProcessLogPojo> getProcessList(List<Object[]> resultList) {
        List<BcProcessLogPojo> list = new ArrayList<>();
        for (Object[] obj : resultList) {
            BcProcessLogPojo pojo = new BcProcessLogPojo();
            pojo.setPrdContId(Convert.toLong(obj[0]));
            pojo.setContentId(Convert.toInt(obj[1]));
            pojo.setContentName(Convert.toStr(obj[2]));
            pojo.setBcId(Convert.toInt(obj[3]));
            pojo.setCType(Convert.toInt(obj[4]));
            pojo.setPubTime(Convert.toStr(obj[5]));
            pojo.setEndTime(Convert.toStr(obj[6]));
            pojo.setCduRation(Convert.toStr(obj[7]));
            pojo.setSpendTime(Convert.toStr(obj[8]));
            pojo.setBcRefuseReason(Convert.toStr(obj[9]));
            list.add(pojo);
        }
        return list;
    }

    public StringBuffer sqlStringWhere(BcRecodePojo entity, String sidx, String sord) {
        StringBuffer condition = new StringBuffer();
        String sql = "SELECT b.PRD_CONT_ID,b.CONTENT_ID,c.NAME ,b.BC_PERSON,i.NAME bokongfang,b.BC_TIME,b.BC_STATUS\n" +
                "FROM BC_LOG b,BC_INFO i,CONTENT c,BC_TIME_LOG t,PRD_INFO p\n" +
                "WHERE b.BC_ID= i.BC_ID\n" +
                "AND i.BC_ID=t.BC_ID\n" +
                "AND t.CONTENT_ID=c.CONTENT_ID\n" +
                "AND p.BC_ID=i.BC_ID ";
        condition.append(sql);
        if (!ObjectUtils.isEmpty(entity)) {
            if (entity.getPrdContId() != 0) {
                condition.append("AND b.PRD_CONT_ID= '" + entity.getPrdContId() + "' ");
            }
            if (entity.getContentId() != 0) {
                condition.append("AND b.CONTENT_ID= '" + entity.getContentId() + "' ");
            }
            if (StrUtil.isNotBlank(entity.getBcName())) {
                condition.append("AND i.NAME LIKE '%" + entity.getBcName() + "%' ");
            }
            if (StrUtil.isNotBlank(entity.getBcPerson())) {
                condition.append("AND b.BC_PERSON LIKE '%" + entity.getBcPerson() + "%' ");
            }
            if (entity.getBcStatus() != 0) {
                condition.append("AND b.BC_STATUS=" + entity.getBcStatus());
            }
            if (StrUtil.isNotBlank(entity.getPrdName())) {
                condition.append("AND c.DISPLAY_NAME LIKE '%" + entity.getDisplayName() + "%'");
            }
            if (StrUtil.isNotBlank(entity.getPrdName())) {
                condition.append("AND p.NAME  LIKE '%" + entity.getPrdName() + "%'");
            }
            if (StrUtil.isNotBlank(entity.getPubhStart())) {
                condition.append("AND b.BC_TIME>='" + entity.getPubhStart() + "' ");
            }
            if (StrUtil.isNotBlank(entity.getPubhEnd())) {
                condition.append("AND b.BC_TIME<='" + entity.getPubhEnd() + "' ");
            }
        }
        condition.append("order by " + sidx + " " + sord);
        return condition;
    }
    public StringBuffer sqlStringWhere(BcProcessLogPojo entity, String sidx, String sord) {
        StringBuffer condition = new StringBuffer();
        String sql = "SELECT b.PRD_CONT_ID,c.CONTENT_ID,c.NAME,b.BC_ID,c.CATEGORY,t.GET_TIME,t.END_TIME,t.CDURATION,t.SPEND_TIME,t.BC_REFUSE_REASON  FROM BC_LOG b,BC_INFO i,CONTENT c,BC_TIME_LOG t WHERE b.BC_ID= i.BC_ID AND i.BC_ID=t.BC_ID AND t.CONTENT_ID=c.CONTENT_ID";
        condition.append(sql);
        if (!ObjectUtils.isEmpty(entity)) {
            if (entity.getPrdContId() != 0) {
                condition.append("AND b.PRD_CONT_ID= '" + entity.getPrdContId() + "' ");
            }
            if (entity.getContentId() != 0) {
                condition.append("AND b.CONTENT_ID= '" + entity.getContentId() + "' ");
            }
            if (StrUtil.isNotBlank(entity.getContentName())) {
                condition.append("AND c.NAME LIKE '%" + entity.getContentName() + "%' ");
            }
            if (entity.getBcId()!=0) {
                condition.append("AND t.BC_ID=" + entity.getBcId());
            }
            if (entity.getBcStatus() != 0) {
                condition.append("AND b.BC_STATUS=" + entity.getBcStatus());
            }
            if (StrUtil.isNotBlank(entity.getPubhStart())) {
                condition.append("AND b.BC_TIME>='" + entity.getPubhStart() + "' ");
            }
            if (StrUtil.isNotBlank(entity.getPubhEnd())) {
                condition.append("AND b.BC_TIME<='" + entity.getPubhEnd() + "' ");
            }
        }
        condition.append(" order by " + sidx + " " + sord);
        return condition;
    }

    @Override
    public BcLog deleteLogic(Long id) {
        return null;
    }

    @Override
    public BcLog merge(BcLog model) {
        return null;
    }
}